function f_update_status_for_task()
{
    declare u_tools u_table u_ip fun_name
    fun_name="f_update_status_for_task"
    u_tools="${1}"
    u_ip="${2}"
    u_table="${3}"
    tmp_chunk_size="${max_chunk_size}"
    tmp_repl_time="${max_repl_time}"
    [ -z "${tmp_osc_run_state}" ] && tmp_osc_run_state="0" || tmp_osc_run_state="${global_osc_run_state}"
    f_check_diy_info "${u_tools}" || true

    if [ "${global_osc_run_state}x" == "1x" ] && [ "${tmp_osc_run_state}x" == "0x" ] && [ "$(grep -ic "^alter_table" <<< "${mode_opt}")x" == "1x" ]
    then
        f_logging "REMAIND:${fun_name}" "'${u_ip}:${mysql_port}' 改表将暂停,操作人[${user_mail_addr}]" "1" "0" "0"
        [ "${u_tools}x" == "gh-ostx" -a -S "${gh_socket_file}" ] && { echo "throttle" | socat - ${gh_socket_file} || true;}
        [ "${u_tools}x" == "pt-online-schema-changex" -a ! -f "${pause_file_name}" ] && touch ${pause_file_name}
        [ "${first_check_mdl_mark}x" == "2x" ] && _1_="" || _1_="改表暂停,操作人[${user_mail_addr}] 待计算"
    elif [ "${global_osc_run_state}x" == "0x" ] && [ "${tmp_osc_run_state}x" == "1x" ] && [ "$(grep -ic "^alter_table" <<< "${mode_opt}")x" == "1x" ]
    then
        f_logging "REMAIND:${fun_name}" "'${u_ip}:${mysql_port}' 改表将继续(如果是pt-osc 将在 1 min后恢复copy操作),操作人[${user_mail_addr}]" "1" "0" "0"
        [ "${u_tools}x" == "gh-ostx" -a -S "${gh_socket_file}" ] && { echo "no-throttle" | socat - ${gh_socket_file} || true;}
        [ "${u_tools}x" == "pt-online-schema-changex" -a -f "${pause_file_name}" ] && rm -f ${pause_file_name}
        [ "${first_check_mdl_mark}x" == "2x" ] && _1_="" || _1_="改表恢复,操作人[${user_mail_addr}] 请稍等"
    elif [ "${global_osc_run_state}x" == "-1x" ] && [ "$(grep -ic "^alter_table" <<< "${mode_opt}")x" == "1x" ]
    then
        _1_="${u_ip}:${mysql_port} 改表将人为终止,操作人[${user_mail_addr}], 如有疑问请联系dba"
        f_logging "REMAIND:${fun_name}" "${_1_}" "1" "0" "0"
        f_record_info_to_dbadb "${u_tools}" "${u_table}" "${_1_}." || true
        _1_="改表终止,操作人[${user_mail_addr}] 如有疑问请联系dba."
        f_kill_osc_process "${u_tools}" "${u_ip}" "${mysql_port}" "${check_db}" "${u_table}" "${mysql_user}" || true
    fi

    if [ -n "${_1_}" ] && [ "$(grep -ic "^alter_table" <<< "${mode_opt}")x" == "1x" ]
    then
        __="insert into ${t_prog_task}(order_id,user_mail,port,table_name,send_state,start_time,remarks) select ${user_order_id},
            '${g_user_mail_addr}',${mysql_port},'${mysql_db}.${u_table}',0,'$(date -d @${script_start_time} "+%F %H:%M:%S")','${_1_}';"
        f_exe_sql_to_dbadb "${__}" "f_update_status_for_task" || true
        unset __ _1_
    fi

    if [ "${tmp_chunk_size}x" != "${max_chunk_size}x" ] && [ "$(grep -ic "^alter_table" <<< "${mode_opt}")x" == "1x" ] && [ "${u_tools}x" == "gh-ostx" ]
    then
        f_logging "REMAIND:${fun_name}" "'${u_ip}:${mysql_port}' 改表修改chunk_size值, 从 '${tmp_chunk_size}' 改为 '${max_chunk_size}' " "1" "0" "0"
        [ -S "${gh_socket_file}" ] && { echo "chunk-size=${max_chunk_size}" | socat - ${gh_socket_file};} || true
    fi

    if [ "${tmp_repl_time}x" != "${max_repl_time}x" ] && [ "$(grep -ic "^alter_table" <<< "${mode_opt}")x" == "1x" ]
    then
        if [ "${u_tools}x" == "gh-ostx" ]
        then
            f_logging "REMAIND:${fun_name}" "'${u_ip}:${mysql_port}' 改表修改延迟值, 从 '${tmp_repl_time}ms' 改为 '${max_repl_time}ms' " "1" "0" "0"
        else
            f_logging "REMAIND:${fun_name}" "'${u_ip}:${mysql_port}' 改表修改延迟值, 从 '${tmp_repl_time}s' 改为 '${max_repl_time}s' " "1" "0" "0"
        fi
        [ "${u_tools}x" == "gh-ostx" -a -S "${gh_socket_file}" ] && { echo "max-lag-millis=$((${max_repl_time}*500))" | socat - ${gh_socket_file};} || true
    fi
    return 0
}

function f_update_status_for_task_from_api()
{ #停掉正在做的改表任务
    log_file="${script_execute_res_log_dir}/update_task_for_alter.log"
    [ -z "${user_order_id}" -a -z "${mysql_port}" ] && exit 1
    [ -z "${1}" ] && exit 1
    declare update_opt show_info insert_opt fun_name
    fun_name="f_update_status_for_task_from_api"
    update_opt="${1}"
    show_info="${2}"
    insert_opt="${3}"
    insert_info="${4}"
    [ -n "${g_user_mail_addr}" ] && update_mail=", user_mail = '${g_user_mail_addr}'"
    if [ "${insert_opt}x" == "insertx" ]
    then
        ___="select concat((select count(*) from ${t_state_task} 
            where order_id = id and port = ${mysql_port}) ,':::',(select max(id) from ${t_state_task}));"
        __="$(${mysql_comm_dbadb} -NBe "${___}" 2>/dev/null)" || true
        if [ "$(grep -vc "^0:::" <<< "${__}")x" == "0x" ]
        then
            __max_id="$(awk -F::: '{print $2+1}' <<< "${__}")"
            __="insert into ${t_state_task}(id,order_id,user_mail,port,chunk_size,run_state,repl_time,big_table,disk_space,osc_tool,
                progress,max_connect,remarks) select ${__max_id},${__max_id},'${g_user_mail_addr}',${mysql_port},3000,0,60,${insert_info},'0%',2500,'';"
            _select_="select count(*) from ${t_state_task} where order_id = ${__max_id};"
        elif [ -z "${__}" ]
        then
            f_exe_sql_to_dbadb "${___}" "${fun_name}" || return 1
        else
            __="update ${t_state_task} set ${update_opt} ${update_mail} where order_id = id and port = ${mysql_port};"
            _select_="select count(*) from ${t_state_task} where order_id = id and port = ${mysql_port};"
        fi
    elif [ "${insert_opt}x" == "selectx" ]
    then
        _select_="select ${update_opt} from ${t_state_task} where order_id = ${user_order_id};"
        res_row="$(${mysql_comm_dbadb} -NBe "${_select_}" 2>/dev/null)" && exe_state="${?}" || exe_state="${?}"
        if [ -n "${res_row}" ]
        then
            f_logging "INFO:${fun_name}" "${res_row}"
            return 0
        elif [ ${exe_state} -eq 0 ]
        then
            f_logging "WARN:${fun_name}" "${show_info},原因是这个工单'${user_order_id}'的记录不存在"
            return 1
        else
            f_exe_sql_to_dbadb "${_select_}" "${fun_name}" || return 1
        fi
    else
        _select_="select count(*) from ${t_state_task} where port = ${mysql_port}"
        __="$(${mysql_comm_dbadb} -NBe "${_select_}" 2>/dev/null)" || true
        if [ -n "${user_order_id}" ] || [ "${__}x" != "0x" ]
        then
            __="update ${t_state_task} set ${update_opt} ${update_mail} where order_id = ${user_order_id};"
            _select_="select count(*) from ${t_state_task} where order_id = ${user_order_id};"
        else
            __max_id="(select ifnull(max(id)+1,1) from ${t_state_task})"
            __="begin;insert into ${t_state_task}(id,order_id,user_mail,port,chunk_size,run_state,repl_time,big_table,disk_space,osc_tool,
                progress,max_connect,remarks) select ${__max_id},${__max_id},'${g_user_mail_addr}',${mysql_port},3000,0,60,0,0,0,'0%',2500,'';
                update ${t_state_task} set ${update_opt} ${update_mail} where port = ${mysql_port};commit;"
            _select_="select count(*) from ${t_state_task} where port = ${mysql_port}"
        fi
    fi

    f_exe_sql_to_dbadb "${__}" "${fun_name}" && exe_state="${?}" || exe_state="${?}"
    if [ ${exe_state} -eq 0 ]
    then
        declare res_row
        res_row="$(${mysql_comm_dbadb} -NBe "${_select_}" 2>/dev/null)" && exe_state="${?}" || exe_state="${?}"
        if [ "${res_row}x" == "1x" ]
        then
            f_logging "INFO:${fun_name}" "${show_info}"
            return 0
        elif [ ${exe_state} -eq 0 ]
        then
            f_logging "WARN:${fun_name}" "${show_info},原因是这个工单'${user_order_id}'的记录不存在"
            return 1
        else
            f_exe_sql_to_dbadb "${_select_}" "${fun_name}" || return 1
        fi
    else
        f_logging "ERROR:${fun_name}" "${show_info}" "2"
        return 1
    fi
    return 0
}

